#include "OneWayPlatform.h"
#include <iostream>
using namespace std;

OneWayPlatform::OneWayPlatform(GameState *gameState, int unitsWide, bool canStepDown):
				PhysicalObject(gameState), 
				m_unitsWide(unitsWide)
{
	addProperty("OneWayPlatform");
	addProperty("oneway");
	addProperty("ground");
	setLayer(4);

	if(canStepDown){
		addProperty("stepdown");
		m_texture = gameState->getTextureManager().getTexture("platformCenter.png");
		m_texture2 = gameState->getTextureManager().getTexture("platformSide.png");
	}else{
		m_texture = gameState->getTextureManager().getTexture("platformCenterIce.png");
		m_texture2 = gameState->getTextureManager().getTexture("platformSideIce.png");
	}

	

	setSize(b2Vec2(unitsWide, (float(m_texture->getSize().y)/m_texture->getSize().x)));

	getBodyDef()->type = b2_kinematicBody;
	getBodyDef()->fixedRotation = true;
	b2FixtureDef *fixDef = new b2FixtureDef;
	fixDef->filter.groupIndex = -2;
	b2PolygonShape *shape = new b2PolygonShape;
	shape->SetAsBox(m_size.x, m_size.y, b2Vec2(0.0f,-1),0);
	fixDef->shape = shape;
	fixDef->density = 2.0f;
	fixDef->restitution = 0.0f;
	addFixtureDef(fixDef);
	setRotation(0.0f);
}


OneWayPlatform::~OneWayPlatform()
{
}


void OneWayPlatform::draw(){

	//drawFixtures();

	b2Vec2 pos = getPosition();

	

	glPushMatrix();
	{
		glTranslatef(pos.x, pos.y-0.8f, 0); 
		glScalef(2, 2, 1);

		sf::Texture::bind(m_texture2); // Border texture
		// Left side
		glPushMatrix();
		{
			glTranslatef( -m_unitsWide/2.0f+0.5f, 0, 0);
			glBegin(GL_QUADS);
			{
				glTexCoord2f(0.0f, 1.0f); glVertex3f( -0.5f, -0.5f, -0.2f);
				glTexCoord2f(1.0f, 1.0f); glVertex3f( 0.5f, -0.5f, -0.2f);
				glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.5f, 0.5f, -0.2f);
				glTexCoord2f(0.0f, 0.0f); glVertex3f( -0.5f, 0.5f, -0.2f);
			}
			glEnd();
		}
		glPopMatrix();

		// Right side
		glPushMatrix();
		{
			glTranslatef( m_unitsWide/2.0f-0.5f, 0, 0);
			glScalef(-1, 1, 1);
			glBegin(GL_QUADS);
			{
				glTexCoord2f(0.0f, 1.0f); glVertex3f( -0.5f, -0.5f, -0.2f);
				glTexCoord2f(1.0f, 1.0f); glVertex3f( 0.5f, -0.5f, -0.2f);
				glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.5f, 0.5f, -0.2f);
				glTexCoord2f(0.0f, 0.0f); glVertex3f( -0.5f, 0.5f, -0.2f);
			}
			glEnd();
		}
		glPopMatrix();

		sf::Texture::bind(m_texture); // Center texture
		// Center
		for(int i = 1; i < m_unitsWide-1; i++){
			glPushMatrix();
			{
				glTranslatef( i-m_unitsWide/2.0f+0.5f, 0, 0);
				glBegin(GL_QUADS);
				{
					glTexCoord2f(0.0f, 1.0f); glVertex3f( -0.5f, -0.5f, -0.2f);
					glTexCoord2f(1.0f, 1.0f); glVertex3f( 0.5f, -0.5f, -0.2f);
					glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.5f, 0.5f, -0.2f);
					glTexCoord2f(0.0f, 0.0f); glVertex3f( -0.5f, 0.5f, -0.2f);
				}
				glEnd();
			}
			glPopMatrix();
		}

		
	}
	glPopMatrix();

	sf::Texture::bind(NULL);
}